var ionicApp = angular.module('e-app', ['ngRoute']).filter(
    'to_trusted', ['$sce', function ($sce) {  
        return function (text) {  
            return $sce.trustAsHtml(text);  
        }  
    }]  
);
ionicApp.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.withCredentials = true;
}]);

ionicApp.controller('ylController',function($scope,$http) {
$scope.moduleList={};
$scope.checkLogin = function(){
        baas_yl.call({
            'http': $http,
            'method': 'get',
            'uri': 'manager/manager/checkLogin',

            'success': function(d) {
            //	console.log("-----555"+d);
            	
		          if(d.login!='logged'){
		             location.href='../user/login.html';
		        }
		          $scope.manageName=d.managerGroup.groupName;
		          $scope.manageId =d.manager_id;
		          $scope.moduleList=d.moduleList;
            }
        });
    };
    $scope.checkLogin();
    $scope.loginout = function(){
        baas_yl.call({
            'http': $http,
            'method': 'post',
            'uri': 'manager/manager/logout',
            'success': function(d) {
            	
		          if(d.login!='logged'){
		          
		             location.href='user/login.html';
		        }
            }
        });
    };
	 	$scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
         dropdown();
	})
});

ionicApp.directive('onFinishRenderFilters', function ($timeout) {
    return {
        restrict: 'A',
        link: function(scope, element, attr) {
            if (scope.$last === true) {
                $timeout(function() {
                    scope.$emit('ngRepeatFinished');
                });
            }
        }
    };
});
	
	
	
	function Baas(){}

Baas.url = {
    base       : 'http://bus.site4test.com/',
    init       : 'console/app/init',
    app_id     : '',
    app_secret : ''
};
Baas.prototype.getApiUrl = function( name ){
    var path = name.replace(/\./g,'/');
    var url = Baas.url.base + path;
    var keyUrl = "?app_id="+Baas.url.app_id + "&app_secret="+Baas.url.app_secret;
    url += keyUrl;
    return url;
};
Baas.prototype.request = function(url, data, requestType, fn){

    $.ajax({
        url : url,
        type: requestType,
        data : data,
        dataType : "json",
        success:function(json){
            if( !json.status ){
                console.log(json.description);
            }
            if( typeof fn == 'function'){
                fn(json);
            }
        }
    });
};

Baas.prototype.requestJsonp = function(url, data, callback){
    $.ajax({
        url : url,
        type:'get',
        data : data,
        dataType : "jsonp",
        jsonp: 'callback',
        jsonpCallback:callback,
        success:function(d){
           console.log(d);
        }
    });
};

Baas.init = function( app_id, app_secret ){

    Baas.url.app_id     = app_id;
    Baas.url.app_secret = app_secret;
    //var data = {
    //    app_id:app_id,
    //    app_secret:app_secret
    //};
    //var bs = new Baas();
    //var url = Baas.url.base + Baas.url.init;
    //bs.request(url, data, function( json ){
    //    if( !json.status ){
    //        console.log(json.description);
    //    }
    //});
};


Baas.prototype.api = function(  ){

    if(typeof arguments[0] == 'undefined'){
        console.log('请填写api名称');
        return;
    }
    var url = this.getApiUrl( arguments[0] );
    var data = ( typeof arguments[1] != 'undefined' ) ? arguments[1] : { };
    var requestType = ( typeof arguments[2] != 'undefined' ) ? arguments[2] : 'get';
    var callback = ( typeof arguments[3] != 'undefined' ) ? arguments[3] : 'callbackz';
    if(requestType == 'jsonp'){

        this.requestJsonp( url, data, callback );
    }else{
        this.request( url, data, requestType, callback );
    }

};
